# -*- coding: utf-8 -*-
"""
"""

from sqlalchemy import *
from sqlalchemy.orm import mapper, relation
from sqlalchemy import Table, ForeignKey, Column
from sqlalchemy.types import Integer, Unicode
#from sqlalchemy.orm import relation, backref

from abserver.model import DeclarativeBase, metadata, DBSession



class Branch(DeclarativeBase):
    """The Branch entity is used to describe all the branches of builds that have been uploaded
    into the abserver.
    """
    # table options
    __tablename__ = 'branch'

    # columns
    branch_id = Column(Integer, primary_key=True, autoincrement=True)
    branch_name = Column(Unicode(64), unique=True, nullable=True)
    branch_description = Column(Unicode(255))

    # relationships
    ## builds
    ## builders
    ## components
    ## buildviews
    ## builderschedules

#    def __init__(self, branch_name, branch_description=None):
#        self.branch_name = branch_name
#        self.branch_description = branch_description
#        self.flush()

    @classmethod
    def get_branch(cls, branch_name, branch_description=''):
        """Convenience getter or factory method."""
        b = DBSession.query(cls).filter(cls.branch_name==branch_name).first()
        if not b:
            b = Branch(branch_name=branch_name, branch_description=branch_description)
            DBSession.add(b)
        return b

    @classmethod
    def get_branch_listing(cls):
        branches = cls.query.all()
        return [ (b.branch_id, b.branch_name) for b in branches ]
